package cn.ctyun.ipush.dao;

import cn.ctyun.ipush.model.ExcelModel;
import cn.ctyun.ipush.utils.DateUtils;
import cn.ctyun.ipush.vo.ExcelVo;
import com.dexcoder.commons.utils.UUIDUtils;
import com.dexcoder.dal.JdbcDao;
import com.dexcoder.dal.build.Criteria;
import com.dexcoder.dal.spring.page.PageControl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: user
 * Date: 16-7-11
 * Time: 下午2:29
 * To change this template use File | Settings | File Templates.
 */
@Component
public class ExcelDao {
    @Autowired
    private JdbcDao jdbcDao;

    @Autowired
    private PageControl pageControl;

    /**
     * 保存
     *
     * @param excelModel
     */
    public void save(ExcelModel excelModel) {
        jdbcDao.save(excelModel);
    }

    /**
     * 更新 ,主键Id 必须存在;此数据是修改后的完整对象
     *
     * @param excelModel
     */
    public int update(ExcelModel excelModel) {
        if (StringUtils.isEmpty(excelModel.getExcelModelId()))
            throw new RuntimeException("excelModel 主键id 必须设置");
        excelModel.setModifyDate(DateUtils.dateToString(new Date()));
        return jdbcDao.update(excelModel);
    }


    /**
     * 更新 主键Id 必须存在
     *
     * @param excelModelId
     */
    public int delete(String excelModelId) {
        if (StringUtils.isEmpty(excelModelId))
            throw new RuntimeException("excelModel 主键id 必须设置");
        Criteria criteria = Criteria.update(ExcelModel.class).where("excelModelId", new Object[]{excelModelId}).set("isDeleted", "1");
        return jdbcDao.update(criteria);
    }

    /**
     * 查询单个,根据ID
     *
     * @param excelModelId
     * @return
     */
    public ExcelModel querySingleResult(String excelModelId) {
        if (StringUtils.isEmpty(excelModelId))
            throw new RuntimeException("excelModel 主键id 必须设置");
        Criteria criteria = Criteria.select(ExcelModel.class).where("excelModelId", new Object[]{excelModelId}).where("and isDeleted", new Object[]{"0"});
        return jdbcDao.querySingleResult(criteria);
    }

    /**
     * 查询单个，根据名称
     *
     * @param excelName
     * @return
     */
    public ExcelModel querySingleResultByName(String excelName) {
        if (StringUtils.isEmpty(excelName))
            throw new RuntimeException("excelName 为空");
        Criteria criteria = Criteria.select(ExcelModel.class).where("excelName", new Object[]{excelName}).where("and isDeleted", new Object[]{"0"});
        return jdbcDao.querySingleResult(criteria);
    }

    /**
     * 模糊查询多个
     *
     * @param criteria
     * @return
     */
    public List<ExcelModel> queryList(Criteria criteria) {
        return jdbcDao.queryList(criteria);
    }

    /**
     * 查询全部
     *
     * @param excelModel
     * @return
     */
    public List<ExcelModel> queryAll(ExcelModel excelModel) {
        return jdbcDao.queryList(excelModel);
    }

    /**
     * 分页按条件查询
     *
     * @param currentPage
     * @param itemPages
     * @param excelVo
     * @return
     */
    public List<ExcelModel> queryListByPage(int currentPage, int itemPages, ExcelVo excelVo) {
        pageControl.performPage(currentPage, itemPages);
        Criteria criteria = Criteria.select(ExcelModel.class).where("excelName", " like ", new Object[]{"%t%"});
        jdbcDao.queryList(criteria);
        return pageControl.getPager().getList(ExcelModel.class);
    }

    public List<ExcelModel> queryListByPageWithUserId(int currentPage, int itemPages, ExcelVo excelVo) {
        pageControl.performPage(currentPage, itemPages);
        Criteria criteria = Criteria.select(ExcelModel.class).where("excelName", " like ", new Object[]{"%" + excelVo.getExcelName() + "%"});
        jdbcDao.queryList(criteria);
        return pageControl.getPager().getList(ExcelModel.class);
    }

    public List<ExcelModel> queryBySql(ExcelModel excelModel) {
        //pageControl.performPage(1, 10);
        excelModel.setExcelName("testdb");
        List<ExcelModel> excelModelList = jdbcDao.queryListForSql("cn.ctyun.ipush.model.ExcelModel.getData", "params", new Object[]{excelModel, null}, ExcelModel.class);
        return excelModelList;
    }

}